package com.nuo.backend.modules.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nuo.backend.common.excel.po.ExportListPo;
import com.nuo.backend.common.excel.service.BaseIService;
import com.nuo.backend.common.excel.service.ExcelService;
import com.nuo.backend.common.utils.BeanUtil;
import com.nuo.backend.common.utils.Constant;
import com.nuo.backend.modules.user.po.LoginPO;
import com.nuo.backend.modules.user.po.PwdPO;
import com.qiniu.util.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nuo.backend.modules.user.entity.UserEntity;
import com.nuo.backend.modules.user.service.UserService;
import com.nuo.backend.common.utils.PageUtils;
import com.nuo.backend.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 *
 *
 * @author nuo
 * @email 3033616351@qq.com
 * @date 2022-12-04 00:51:20
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ExcelService<UserEntity, BaseIService<UserEntity>> excelService;

    /**
     * 登陆
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginPO po) {
        return userService.login(po);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public R register(@RequestBody LoginPO po) {
        if (userService.getOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUserId, po.getUserId())) != null) {
            return R.error("当前账户已注册...");
        }
        po.setPassword(new Sha256Hash(po.getPassword()).toHex());
        userService.save(BeanUtil.beanAtoBeanB(po, UserEntity.class));
        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping
    public R save(@RequestBody UserEntity user) {
        if (userService.getOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUserId, user.getUserId())) != null) {
            return R.error("当前账户已注册...");
        }
        userService.save(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 修改密码
     */
    @PostMapping("/updatePwd")
    public R updatePwd(@RequestBody PwdPO po) {
        UserEntity user = userService.getById(po.getId());
        if (!user.getPassword().equals(new Sha256Hash(po.getOldPwd()).toHex())) {
            return R.error("原密码输入有误...");
        }
        if (StringUtils.isNullOrEmpty(po.getNewPwd())) {
            return R.error("新密码不能为空...");
        }
        user.setPassword(new Sha256Hash(po.getNewPwd()).toHex());
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 重置密码
     */
    @PutMapping("/reset")
    public R reset(@RequestBody UserEntity user) {
        user.setPassword(new Sha256Hash(Constant.DEFAULT_PWD).toHex());
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 信息
     */
    @GetMapping("/{id}")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);
        return R.ok().put("user", user);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 导出全部用户信息
     */
    @RequestMapping("/exportAll")
    public R exportAll() {
        return excelService.exportAll(UserEntity.class, userService);
    }

    /**
     * 导出用户信息
     */
    @RequestMapping("/export")
    public R export(@RequestBody ExportListPo<UserEntity> po) {
        return excelService.exportList(po, UserEntity.class);
    }

    /**
     * 导入用户信息
     */
    @RequestMapping("/importUser")
    public R importUser(@RequestParam("file") MultipartFile file) {
        return excelService.importList(file, UserEntity.class, userService);
    }

}
